package com.app.qbake;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import com.app.qbake.module.mine.event.H5PaySuccessEvent;
import com.umeng.analytics.AnalyticsConfig;
import com.wind.base.AbsH5Activity;
import com.wind.base.H5Param;
import com.wind.base.utils.ActivityUtil;
import com.wind.base.utils.AlipayUtil;
import com.wind.base.utils.WebViewHelper;
import com.wind.umengsharelib.UmengUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by wind on 2018/11/10.
 */

public class H5Activity extends AbsH5Activity {

    public static void start(Context context, H5Param param) {
        Intent intent = new Intent(context, H5Activity.class);
        intent.putExtra(ARG_KEY_H5PARAM, param);
        context.startActivity(intent);
    }
    @Override
    public WebViewHelper.ProtocolHandler getProtocolHandler() {
        return new WebViewHelper.ProtocolHandler() {
            @Override
            public void handle(String url) {
                 if (url.startsWith("alipays")) {
                    // 打开支付宝客户端。
                    if (url.contains("alipays://platformapi")) {
                        boolean visit = AlipayUtil.checkAliPayInstalled(getActivity());
                        if (visit) {
                            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                            startActivity(intent);
                        }
                    }

                } else if (url.contains("alipayReturn")) {//http://cargo.dsweixin.cc/v1/pay/alipayReturn?charset=UTF-8&out_trade_no=2540595197709484135&method=alipay.trade.wap.pay.return&total_amount=0
                     webView.loadUrl(url);
                }else {
                     webView.loadUrl(url);
                 }
            }
        };
    }



    public void onPaySuccess() {
        EventBus.getDefault().post(new H5PaySuccessEvent());
        try {
            SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
            String value="";
            if (sp != null) {
                value = sp.getString("user_uid", "");

            }
            String channel=AnalyticsConfig.getChannel(this);
            UmengUtil.onEvent(this,"Vip",channel+"_"+value+"_payed");
        }catch (Exception e){
            e.printStackTrace();
        }

        ActivityUtil.finish(getActivity());
    }
}
